Ext.define('AddressInfo', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
		, "AddressType"   //地址类型
		, "Nation"   //国家
		, "Province"   //省份
		, "City"   //市
		, "County"   //区县
		, "Street"   //街道
		, "Other"   //其他
		, "PostalCode"   //邮政编码
		, "CustomerId"   //客户Id
		, "CustomerName"   //客户名称
		, "EmpId"   //员工Id
		, "EmpName"   //员工姓名
        , "AddressCategory" //地址类别：1为公司，2为个人
	],
    idProperty: "Id"
});

Ext.define('SHApp.EmployeeInfo.AddressInfo.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'shapp.employeeinfo.grid',
    requires: ['SHApp.EmployeeInfo.AddressInfo.Form'],
    txtAddressTypeID: 'txtAddressType',
    txtNationID: 'txtNation',
    txtProvinceID: 'txtProvince',
    dataModel: "AddressInfo",
    dataUrl: "api/addressinfo?format=json",
    empId: "",
    empName: "",
    customerId: "",
    customerName: "",
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.txtAddressTypeID += me.id;
        me.txtNationID += me.id;
        me.txtProvinceID += me.id;
        this.creatTbar();
        this.creatColumns();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "Id",
            flex: 1,
            dataIndex: 'Id',
            sortable: false,
            hidden: true
        }, {
            text: "地址类型",
            width: 100,
            dataIndex: 'AddressType',
            sortable: true
        }, {
            text: "国家",
            width: 100,
            dataIndex: 'Nation',
            sortable: true
        }, {
            text: "省份",
            width: 100,
            dataIndex: 'Province',
            sortable: true
        }, {
            text: "市",
            width: 100,
            dataIndex: 'City',
            sortable: true
        }, {
            text: "区县",
            width: 100,
            dataIndex: 'County',
            sortable: true
        }, {
            text: "街道",
            width: 100,
            dataIndex: 'Street',
            sortable: true
        }, {
            text: "其他",
            width: 100,
            dataIndex: 'Other',
            sortable: true
        }, {
            text: "邮政编码",
            width: 100,
            dataIndex: 'PostalCode',
            sortable: true
        }]
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [
            Ext.create("BLBTSystem.CmbYesNo", {
                fieldLabel: "地址类型",
                fields: ["text"],
                data: [["家庭地址"], ["工作地址"], ["公司地址"], ["邮寄地址"]],
                valueField: "text",
                displayField: "text",
                name: "AddressType",
                id: me.txtAddressTypeID,
                maxLength: 50,
                labelWidth: 60,
                width: 180,
                value: "家庭地址",
                forceSelection: false
            }), {

                xtype: 'textfield',
                fieldLabel: '国家:',
                labelWidth: 40,
                name: 'Nation',
                id: me.txtNationID,
                width: 160
            }, {

                xtype: 'textfield',
                fieldLabel: '省份:',
                labelWidth: 40,
                name: 'Province',
                id: me.txtProvinceID,
                width: 160
            }, ' ', {
                xtype: 'button',
                text: '查询',
                iconCls: 'icon-btn-search',
                handler: function () {
                    me.clearFilters();
                    me.addFilter("EmpId", me.empId);
                    me.addFilter("AddressCategory", 2);
                    var vAddressType = me.down("#" + me.txtAddressTypeID).getValue();
                    if (vAddressType && vAddressType.length > 0) {
                        me.addFilter("AddressType", vAddressType);
                    }

                    var vNation = me.down("#" + me.txtNationID).getValue();
                    if (vNation && vNation.length > 0) {
                        me.addFilter("Nation", vNation);
                    }

                    var vProvince = me.down("#" + me.txtProvinceID).getValue();
                    if (vProvince && vProvince.length > 0) {
                        me.addFilter("Province", vProvince);
                    }
                    me.loadByFilter();
                }
            }, '->', {
                xtype: 'button',
                text: '添加',
                id: me.btnAddID,
                iconCls: 'icon-btn-add',
                handler: function () {
                    var win = me.getForm('SHApp.EmployeeInfo.AddressInfo.Form');
                    win.loadRecord(new AddressInfo({ Id: null, AddressType: "家庭地址", EmpId: me.empId, EmpName: me.empName, CustomerId: me.customerId, CustomerName: me.customerName, AddressCategory: 2 }));
                    win.Show("添加");
                }
            }, ' ', {
                xtype: 'button',
                disabled: true,
                id: me.btnEditID,
                text: '修改',
                iconCls: 'icon-btn-edit',
                handler: function () {
                    var rs = me.getSelectionModel().getSelection();
                    if (rs) {
                        if (rs.length > 0) {
                            // rs[0].data.StartDate = formatDate(rs[0].data.StartDate);                           
                            me.EditData(me.getForm('SHApp.EmployeeInfo.AddressInfo.Form'));
                        }
                    }
                }
            }, ' ', {
                xtype: 'button',
                disabled: true,
                id: me.btnDeleteID,
                text: '删除',
                iconCls: 'icon-btn-delete',
                handler: function () {
                    me.DeleteData();
                }
            }
        ]
    }
})

